package com.wzqj.agent.app.device.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.app.device.service.ApiDeviceService;
import com.wzqj.agent.common.JsonOutput;
import com.wzqj.agent.common.Log;
import com.wzqj.agent.common.exception.BusinessErrorType;
import com.wzqj.agent.util.StoreUtil;

@Controller
@RequestMapping("/api/device")
public class ApiDeviceController {

	@Autowired
	private ApiDeviceService apiDeviceService;
	
	 /**
     * 绑定设备到店铺
     * @param en 设备En号
     * @param mcode 商户mcode
     * @return
     */
    @RequestMapping("/bindDevice")
    @ResponseBody
    public JSONObject bindDevice(HttpServletRequest req,String mcode,String en){

        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(mcode)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        if(StringUtils.isEmpty(en)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        JSONObject data = apiDeviceService.bindDeviceToStore(mcode, en);
        if(data!= null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.DEVICE_BIND_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.DEVICE_BIND_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }
}
